import java.util.Date;
import java.util.ArrayList;
import java.util.List;
import java.time.ZoneId;
import java.time.LocalDateTime;
public class FrontDesk extends User {
    private List<Movie> showingMovies = new ArrayList<>();
    private List<Ticket> soldTickets = new ArrayList<>();
    private List<Ticket>refundedTickets=new ArrayList<>();
    private List<Movie>movies=new ArrayList<>();
    private List<Ticket>activeTickets=new ArrayList<>();
    private List<Screening>screenings=new ArrayList<>();
    public FrontDesk(String userId, String username, String password, Date registerTime, String phone) {
        super(userId, username, password, registerTime, UserType.FRONT_DESK, phone);
    }
    public void addScreening(Screening screenings){
        this.screenings.add(screenings);
    }
    public void addMovie(Movie movie){
        this.movies.add(movie);
    }
    public List<Movie> listAllShowingMovies() {
        return new ArrayList<>(movies);
    }
    public List<Movie> listShowingMovies() {
        return showingMovies;
    }
    public List<Screening> listScreenings(String scope) {//筛选排期
        List<Screening> allScreenings = this.screenings;
        List<Screening> filteredScreenings = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        for (Screening screening : allScreenings) {
            Date date=screening.getTime();
            LocalDateTime screeningTime =date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            if ("today".equals(scope)) {
                if (screeningTime.toLocalDate().equals(now.toLocalDate())) {
                    filteredScreenings.add(screening);
                }
            } else if ("3days".equals(scope)) {
                if (!screeningTime.isAfter(now.plusDays(3))) {
                    filteredScreenings.add(screening);
                }
            } else if ("week".equals(scope)) {
                if (!screeningTime.isAfter(now.plusWeeks(1))) {
                    filteredScreenings.add(screening);
                }
            }
        }

        return filteredScreenings;
    }
    private static Seat[][] convertListTo2DArray(List<Seat> seatList, int rows, int cols) {//将座位转换为二维数组
        Seat[][] seats = new Seat[rows][cols];
        for (int i = 0; i < seatList.size(); i++) {
            int row = i / cols;
            int col = i % cols;
            seats[row][col] = seatList.get(i);
        }
        return seats;
    }
    public List<Screening>listScreenings(){
        return new ArrayList<>(this.screenings);
    }
    public Screening queryMovieScreeningsByTitle(String movieName) {//按影片标题查询排期
        List<Screening> result = new ArrayList<>();
        for (Screening screening : listScreenings()) {
            if (screening.getMovie() != null && screening.getMovie().getTitle().equals(movieName)) {
                result.add(screening);
            }
        }
        return result.isEmpty()?null:result.get(0);
    }
    public Ticket sellTicket(Screening screening, List<Seat> selectedSeats) {
        double totalPrice = selectedSeats.size() * screening.getPrice();
        for (Seat seat : selectedSeats) {
            if (!screening.sellTicket(seat.getRow(), seat.getCol())) {
                throw new RuntimeException("座位 " + seat.getRow() + "," + seat.getCol() + " 已售");
            }
        }
        Log log = new Log("LOG-" + System.currentTimeMillis(), new Date(), "SELL_TICKET", this.getUsername());
        log.recordLog("Sold ticket for movie: " + screening.getMovie().getTitle());
        Ticket ticket = new Ticket("TICKET-" + System.currentTimeMillis(), screening,selectedSeats , 0.0,new Date(),0,0.0,0,0.0);
        soldTickets.add(ticket);
        return ticket;
    }
    private void removeTicket(Ticket ticket) {//移除门票
        soldTickets.remove(ticket);
    }
    public void addTicket(Ticket ticket) {//添加门票
        if (ticket != null && ticket.getTicketId() != null) {
            for (Ticket t : activeTickets) {
                if (t.getTicketId().equalsIgnoreCase(ticket.getTicketId().trim())) {
                    System.out.println(" 票号 " + ticket.getTicketId() + " 已存在！");
                    return;
                }
            }
            activeTickets.add(ticket);
            System.out.println("票号 " + ticket.getTicketId() + " 已添加到系统");

        }
    }
    public boolean refundTicket(Ticket ticket, double refundAmount) {
        if (ticket == null) return false;
        if (activeTickets.remove(ticket)) {
            ticket.setRefundAmount(refundAmount);
            ticket.setBuyTime(new Date());
            refundedTickets.add(ticket);
            System.out.println("票号 " + ticket.getTicketId() + " 已退票");
            return true;
        }
        System.out.println("退票失败：票号 " + ticket.getTicketId() + " 不在有效票列表中");
        return false;
    }
    public Ticket findTicketById(String ticketId) {//按票号查找门票
        String cleanId = ticketId.trim();
        System.out.println(" 正在查找票号: " + cleanId);
        for (Ticket ticket : activeTickets) {
            if (ticket.getTicketId().equals(cleanId)) {
                System.out.println(" 精确匹配成功");
                return ticket;
            }
        }
        for (Ticket ticket : activeTickets) {
            if (ticket.getTicketId().equalsIgnoreCase(cleanId)) {
                System.out.println(" 不区分大小写匹配成功");
                return ticket;
            }
        }
        for (Ticket ticket : activeTickets) {
            if (ticket.getTicketId().replaceAll("\\s", "").equalsIgnoreCase(cleanId.replaceAll("\\s", ""))) {
                System.out.println(" 去除空格后匹配成功");
                return ticket;
            }
        }
        System.out.println(" 未找到票号: " + cleanId);
        return null;
    }
    public List<Screening>getScreenings(){
        return screenings;
    }
    public List<Movie>getMovies(){
        return movies;
    }
    public List<Ticket>getSoldTickets(){
        return soldTickets;
    }
    public List<Ticket>getRefundedTickets(){
        return refundedTickets;
    }
    public List<Ticket>getActiveTickets(){
        return activeTickets;
    }
    @Override
    public void login() {
        System.out.println("FrontDesk " + getUsername() + " logged in.");
    }
    @Override
    public void logout() {
        System.out.println("FrontDesk " + getUsername() + " logged out.");
    }
    @Override
    public void managePassword() {
        System.out.println("FrontDesk " + getUsername() + " is managing password.");
    }
}

